package com.example.cinema.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.cinema.R;
import com.example.cinema.UserAttribute;
import com.example.cinema.bindingdata.SignData;
import com.example.cinema.database.AppDataBase;
import com.example.cinema.databinding.ActivitySignBinding;
import com.example.cinema.request.RequestSend;
import com.example.cinema.toolclass.SvgaUtils;
import com.opensource.svgaplayer.SVGAImageView;

public class ActivitySign extends AppCompatActivity {


    private ActivitySignBinding ASB;
    private SignData signData;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ASB = DataBindingUtil.setContentView(this, R.layout.activity_sign);
        ASB.setSigndata(signData);
        init();
        initlistener();
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = getWindow().getDecorView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }
    }
    private UserAttribute attribute;
    private void init() {
        attribute = (UserAttribute) getApplication();
    }
    boolean aBoolean = false;
    private void initlistener() {
        //登录点击
        ASB.signbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = ASB.phone.getText().toString();
                String paswd = ASB.paswd.getText().toString();
                Log.i("KKKK",phone);
                Log.i("KKKK",paswd);
                Mask();
                new Thread()
                {
                    @Override
                    public void run() {
                        TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                        String id = manager.getDeviceId();
                        Log.i("QQQS",id);
                        RequestSend send = new RequestSend(getApplicationContext());
                        aBoolean = send.UserLong(phone,paswd,attribute,id);
                        Log.i("GGG","登录结果"+aBoolean);
                        if(aBoolean)
                        {
                            boolean b = send.VipCheck(attribute.getUID());
                            attribute.setVipbool(b);
                            AppDataBase appDataBase =new AppDataBase(getApplicationContext(),"userinfo",null,2);
                            SQLiteDatabase sqLiteDatabase = appDataBase.getWritableDatabase();
                            sqLiteDatabase.execSQL("DELETE FROM userinfo");
                            ContentValues values = new ContentValues();
                            values.put("usersid",attribute.getUID());
                            values.put("useralias",attribute.getUalias());
                            values.put("username",attribute.getUname());
                            values.put("userpswd",paswd);
                            values.put("userimg",attribute.getUimage());
                            values.put("usertime",attribute.getViptime());
                            values.put("userfree",attribute.getFree());
                            sqLiteDatabase.insert("userinfo",null,values);
                        }

                        handler.sendEmptyMessage(1000);
                    }
                }.start();
            }
        });
        //忘记点击
        ASB.forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("retrieve");
                startActivity(intent);
            }
        });
        //返回点击
        ASB.finsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //注册点击
        ASB.post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("userpost");
                startActivity(intent);
            }
        });
    }

    private SvgaUtils svgaUtils;
    private AlertDialog dialog;
    private void Mask()
    {
        View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.post_longing,null);
        SVGAImageView svga = view.findViewById(R.id.svgimgs);
        svgaUtils = new SvgaUtils(getApplicationContext(),svga);

        dialog = new AlertDialog.Builder(this).create();
        dialog.setView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        svgaUtils.init();
        svgaUtils.startAnimator("longing");
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.getDecorView().setBackgroundColor(Color.argb(0,0,0,0));
    }

    Handler handler = new Handler(Looper.getMainLooper())
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what==1000)
            {
                if(aBoolean)
                {
                    Toast.makeText(getApplicationContext(),"欢迎回家！",Toast.LENGTH_LONG).show();
                    dialog.cancel();
                    svgaUtils.stopSvga();
//                    Intent intent = new Intent();
//                    intent.setAction("maininlet");
//                    startActivity(intent);
                    finish();
                    return;
                }else {
                    Toast.makeText(getApplicationContext(),"登录失败！",Toast.LENGTH_LONG).show();
                    dialog.cancel();
                    svgaUtils.stopSvga();
                }
            }
        }
    };
}
